<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;

class CreateBasicTables extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        //
        if (!Schema::hasTable('app')) {
            Schema::create('app', function (Blueprint $t) {
                $t->increments('id');
                $t->string('name', 64)->comment('应用名称');
                $t->string('host', 64)->comment('访问地址');
                $t->string('key', 64)->comment('key');
                $t->string('secret', 64)->comment('秘钥');
                $t->string('desc')->comment('描述');
                $t->tinyInteger('status')->comment('状态');
                $t->string('operator_id')->comment('操作用户ID');
                $t->string('operator_name')->comment('操作用户');
                $t->timestamps();
                $t->integer('deleted_at')->default(0)->comment('软删除时间');
                $t->index(['name']);
            });
        }
        //
        if (!Schema::hasTable('app_group')) {
            Schema::create('app_group', function (Blueprint $t) {
                $t->increments('id');
                $t->string('name', 64)->comment('组名称');
                $t->integer('app_id')->comment('关联应用ID');
                $t->string('desc')->comment('描述');
                $t->tinyInteger('status')->comment('状态');
                $t->string('operator_id')->comment('操作用户ID');
                $t->string('operator_name')->comment('操作用户');
                $t->timestamps();
                $t->softDeletes();
                $t->index(['app_id']);
            });
        }
        //
        if (!Schema::hasTable('app_group_extra')) {
            Schema::create('app_group_extra', function (Blueprint $t) {
                $t->increments('id');
                $t->integer('group_id')->comment('关联组ID');
                $t->string('name', 64)->comment('参数名称');
                $t->string('key', 64)->comment('参数键值');
                $t->string('value', 64)->comment('参数值');
                $t->string('desc')->comment('描述');
                $t->string('operator_id')->comment('操作用户ID');
                $t->string('operator_name')->comment('操作用户');
                $t->timestamps();
                $t->softDeletes();
                $t->index(['name']);
            });
        }
        //
        if (!Schema::hasTable('app_auth')) {
            Schema::create('app_auth', function (Blueprint $t) {
                $t->increments('id');
                $t->integer('parent_id')->default(0)->comment('父类Id');
                $t->integer('app_id')->comment('关联应用ID');
                $t->string('name', 64)->comment('权限名称');
                $t->string('uri', 64)->comment('权限路由');
                $t->string('css', 64)->comment('自定义css');
                $t->tinyInteger('is_show')->default(1)->comment('是否显示');
                $t->tinyInteger('weight')->default(0)->comment('权重');
                $t->string('desc')->comment('描述');
                $t->tinyInteger('status')->comment('状态');
                $t->string('operator_id')->comment('操作用户ID');
                $t->string('operator_name')->comment('操作用户');
                $t->timestamps();
                $t->softDeletes();
                $t->index(['app_id']);
            });
        }
        //
        if (!Schema::hasTable('app_group_auth_relation')) {
            Schema::create('app_group_auth_relation', function (Blueprint $t) {
                $t->increments('id');
                $t->unsignedInteger('group_id')->foreign('group_id')->references('id')->on('app_group')->comment('应用权限组ID');
                $t->unsignedInteger('auth_id')->foreign('auth_id')->references('id')->on('app_auth')->comment('权限ID');
                $t->tinyInteger('status')->default(1)->comment('状态');
                $t->string('operator_id')->comment('操作用户ID');
                $t->string('operator_name')->comment('操作用户');
                $t->timestamps();
                $t->softDeletes();
                $t->index(['group_id', 'auth_id']);
            });
        }
        //
        if (!Schema::hasTable('staff')) {
            Schema::create('staff', function (Blueprint $t) {
                $t->increments('id');
                $t->string('name', 64)->comment('员工名称');
                $t->string('account', 64)->comment('登录账号');
                $t->string('mobile', 64)->comment('手机');
                $t->string('email', 64)->comment('邮箱');
                $t->string('password', 64)->comment('秘钥');
                $t->enum('station_id', ['yj', 'yx', 'yc'])->default('yj')->comment('站场ID,用于识别数据库');
                $t->string('dept_id')->comment('部门ID');
                $t->string('post_id')->comment('岗位ID');
                $t->tinyInteger('status')->comment('状态');
                $t->string('operator_id')->comment('操作用户ID');
                $t->string('operator_name')->comment('操作用户');
                $t->timestamps();
                $t->softDeletes();
                $t->index(['name', 'account', 'password']);
                $t->index(['mobile', 'email']);
            });
        }
        //
        if (!Schema::hasTable('staff_dept')) {
            Schema::create('staff_dept', function (Blueprint $t) {
                $t->increments('id');
                $t->string('name', 64)->comment('部门名称');
                $t->string('telephone', 64)->comment('部门电话');
                $t->string('leader', 64)->comment('部门领导');
                $t->string('desc')->comment('简介');
                $t->tinyInteger('status')->comment('状态');
                $t->string('operator_id')->comment('操作用户ID');
                $t->string('operator_name')->comment('操作用户');
                $t->timestamps();
                $t->softDeletes();
                $t->index(['name']);
            });
        }
        //
        if (!Schema::hasTable('staff_dept_extra')) {
            Schema::create('staff_dept_extra', function (Blueprint $t) {
                $t->increments('id');
                $t->integer('post_id')->comment('关联部门ID');
                $t->string('name', 64)->comment('参数名称');
                $t->string('key', 64)->comment('参数键值');
                $t->string('value', 64)->comment('参数值');
                $t->string('desc')->comment('描述');
                $t->string('operator_id')->comment('操作用户ID');
                $t->string('operator_name')->comment('操作用户');
                $t->timestamps();
                $t->softDeletes();
                $t->index(['name']);
            });
        }
        //
        if (!Schema::hasTable('staff_post')) {
            Schema::create('staff_post', function (Blueprint $t) {
                $t->increments('id');
                $t->string('name', 64)->comment('岗位名称');
                $t->string('desc')->comment('描述');
                $t->string('code')->comment('编码');
                $t->tinyInteger('status')->comment('状态');
                $t->string('operator_id')->comment('操作用户ID');
                $t->string('operator_name')->comment('操作用户');
                $t->timestamps();
                $t->softDeletes();
                $t->index(['name']);
            });
        }
        //
        if (!Schema::hasTable('staff_post_extra')) {
            Schema::create('staff_post_extra', function (Blueprint $t) {
                $t->increments('id');
                $t->integer('post_id')->comment('关联岗位ID');
                $t->string('name', 64)->comment('参数名称');
                $t->string('key', 64)->comment('参数键值');
                $t->string('value', 64)->comment('参数值');
                $t->string('desc')->comment('描述');
                $t->string('operator_id')->comment('操作用户ID');
                $t->string('operator_name')->comment('操作用户');
                $t->timestamps();
                $t->softDeletes();
                $t->index(['name']);
            });
        }
        //
        if (!Schema::hasTable('staff_app_relation')) {
            Schema::create('staff_app_relation', function (Blueprint $t) {
                $t->increments('id');
                $t->unsignedInteger('staff_id')->foreign('staff_id')->references('id')->on('staff')->comment('员工ID');
                $t->unsignedInteger('app_id')->foreign('app_id')->references('id')->on('app')->comment('应用ID');
                $t->tinyInteger('status')->default(1)->comment('状态');
                $t->string('operator_id')->comment('操作用户ID');
                $t->string('operator_name')->comment('操作用户');
                $t->timestamps();
                $t->softDeletes();
                $t->index(['staff_id', 'app_id']);
            });
        }
        //
        if (!Schema::hasTable('staff_app_group_relation')) {
            Schema::create('staff_app_group_relation', function (Blueprint $t) {
                $t->increments('id');
                $t->unsignedInteger('staff_id')->foreign('staff_id')->references('id')->on('staff')->comment('员工ID');
                $t->unsignedInteger('app_id')->foreign('app_id')->references('id')->on('app')->comment('应用ID');
                $t->unsignedInteger('group_id')->foreign('group_id')->references('id')->on('group')->comment('应用组ID');
                $t->string('operator_id')->comment('操作用户ID');
                $t->string('operator_name')->comment('操作用户');
                $t->timestamps();
                $t->softDeletes();
                $t->index(['staff_id', 'app_id', 'group_id']);
            });
        }
        //
        if (!Schema::hasTable('admin')) {
            Schema::create('admin', function (Blueprint $t) {
                $t->increments('id');
                $t->string('name', 64)->comment('管理名称');
                $t->string('account', 64)->comment('登录账号');
                $t->string('mobile', 64)->comment('手机');
                $t->string('email', 64)->comment('邮箱');
                $t->string('password', 64)->comment('密码');
                $t->tinyInteger('status')->comment('状态');
                $t->string('remarks')->nullable()->comment('备注信息');
                $t->string('operator_id')->comment('操作用户ID');
                $t->string('operator_name')->comment('操作用户');
                $t->timestamps();
                $t->softDeletes();
                $t->index(['name', 'account']);
            });
        }
        // if (!Schema::hasTable('apps')) {
        //     Schema::create('apps', function (Blueprint $t) {
        //         $t->increments('id');
        //         $t->string('name', 64)->comment('应用名称');
        //         $t->string('desc')->comment('描述');
        //         $t->timestamps();
        //         $t->softDeletes();
        //     });
        // }

        // if (!Schema::hasTable('app_domains')) {
        //     Schema::create('app_domains', function (Blueprint $t) {
        //         $t->increments('id');
        //         $t->unsignedInteger('app_id')->foreign('app_id')->references('id')->on('apps')->onDelete('cascade');
        //         $t->string('domain', 128)->comment('域名');
        //         $t->string('desc', 256)->comment('描述');
        //         $t->timestamps();
        //         $t->softDeletes();
        //         $t->index('domain');
        //     });
        // }

        // if (!Schema::hasTable('app_config')) {
        //     Schema::create('app_config', function (Blueprint $t) {
        //         $t->increments('id');
        //         $t->unsignedInteger('app_id')->foreign('app_id')->references('id')->on('apps')->onDelete('cascade');
        //         $t->string('appid', 20)->comment('微信APPID');
        //         $t->string('appsecret', 32)->comment('微信APPSECRET');
        //         $t->string('aes_key', 43)->comment('微信AESEncodeKey');
        //         $t->string('token', 32)->comment('微信Token');
        //         $t->string('desc', 256)->comment('注释');
        //         $t->unsignedTinyInteger('active')->default(1)->comment('App状态 1=启用');
        //         $t->timestamps();
        //         $t->softDeletes();
        //     });
        // }

        // // 文字 自动回复
        // if (!Schema::hasTable('reply_words')) {
        //     Schema::create('reply_words', function (Blueprint $t) {
        //         $t->increments('id');
        //         $t->unsignedInteger('app_id')->foreign('app_id')->references('id')->on('apps')->onDelete('cascade');
        //         $t->enum('reply_type', ['text', 'image', 'voice', 'video', 'music', 'news'])->comment('回复类型');
        //         $t->string('name', 64)->comment('名称');
        //         $t->string('desc', 256)->comment('注释');
        //         $t->timestamps();
        //         $t->softDeletes();
        //     });
        // }

        // if (!Schema::hasTable('reply_keyword')) {
        //     Schema::create('reply_keyword', function (Blueprint $t) {
        //         $t->increments('id');
        //         $t->unsignedInteger('app_id')->foreign('app_id')->references('id')->on('apps')->onDelete('cascade');
        //         $t->unsignedInteger('rule_id')->foreign('rule_id')->references('id')->on('reply_words')->onDelete('cascade');
        //         $t->string('keyword', 32)->comment('关键字');
        //         $t->timestamps();
        //         $t->softDeletes();
        //     });
        // }

        // // 扫码 自动回复
        // if (!Schema::hasTable('reply_qrcode_type')) {
        //     Schema::create('reply_qrcode_type', function (Blueprint $t) {
        //         $t->increments('id');
        //         $t->unsignedInteger('app_id')->foreign('app_id')->references('id')->on('apps')->onDelete('cascade');
        //         $t->string('name', 64)->comment('名称');
        //         $t->timestamps();
        //         $t->softDeletes();
        //     });
        // }
        // if (!Schema::hasTable('reply_qrcode_params')) {
        //     Schema::create('reply_qrcode_params', function (Blueprint $t) {
        //         $t->increments('id');
        //         $t->unsignedInteger('app_id')->foreign('app_id')->references('id')->on('apps')->onDelete('cascade');
        //         $t->unsignedInteger('act_id')->foreign('act_id')->references('id')->on('reply_qrcode_type')->onDelete('cascade');
        //         $t->enum('reply_type', ['text', 'image', 'voice', 'video', 'music', 'news'])->comment('回复类型');
        //         $t->enum('type', ['subscribe', 'SCAN'])->comment('事件类型');
        //         $t->string('cal_fn', 128)->nullable()->comment('统计算法');
        //         $t->string('data_fn', 128)->nullable();
        //         $t->timestamps();
        //         $t->softDeletes();
        //     });
        // }

        // // 图文消息
        // if (!Schema::hasTable('reply_msgs')) {
        //     Schema::create('reply_msgs', function (Blueprint $t) {
        //         $t->increments('id');
        //         $t->unsignedInteger('app_id')->foreign('app_id')->references('id')->on('apps')->onDelete('cascade');
        //         $t->enum('type', ['news', 'text'])->comment('消息类型');
        //         $t->string('title', 64)->nullable();
        //         $t->text('content')->nullable();
        //         $t->string('desc', 128)->nullable();
        //         $t->string('url', 1024)->nullable();
        //         $t->string('pic_url', 1024)->nullable();
        //         $t->unsignedTinyInteger('weight')->default(0)->comment('比重');
        //         $t->unsignedTinyInteger('is_all')->default(1)->comment('是否全部适用');
        //         $t->unsignedBigInteger('start_time')->nullable()->comment('开始时间');
        //         $t->unsignedBigInteger('end_time')->nullable()->comment('结束时间');
        //         $t->timestamps();
        //         $t->softDeletes();
        //         $t->index(['app_id', 'type']);
        //     });
        // }
        // if (!Schema::hasTable('reply_msg_filters')) {
        //     Schema::create('reply_msg_filters', function (Blueprint $t) {
        //         $t->increments('id');
        //         $t->unsignedInteger('app_id')->foreign('app_id')->references('id')->on('apps')->onDelete('cascade');
        //         $t->unsignedInteger('act_id')->comment('reply_qrcode_type id');
        //         $t->unsignedInteger('act_val')->comment('扫码参数');
        //         $t->timestamps();
        //         $t->softDeletes();
        //     });
        // }

        // // 关系表
        // if (!Schema::hasTable('msgs_words')) {
        //     Schema::create('msgs_words', function (Blueprint $t) {
        //         $t->unsignedBigInteger('msg_id')->index()->foreign('msg_id')->references('id')->on('reply_msgs')->onDelete('cascade');
        //         $t->unsignedBigInteger('rule_id')->index()->foreign('rule_id')->references('id')->on('reply_words')->onDelete('cascade');
        //     });
        // }
        // if (!Schema::hasTable('msgs_qrcodes')) {
        //     Schema::create('msgs_qrcodes', function (Blueprint $t) {
        //         $t->unsignedBigInteger('msg_id')->index()->foreign('msg_id')->references('id')->on('reply_msgs')->onDelete('cascade');
        //         $t->unsignedBigInteger('rule_id')->index()->foreign('rule_id')->references('id')->on('reply_qrcode_type')->onDelete('cascade');
        //     });
        // }
        // if (!Schema::hasTable('msgs_filters')) {
        //     Schema::create('msgs_filters', function (Blueprint $t) {
        //         $t->unsignedBigInteger('msg_id')->index()->foreign('msg_id')->references('id')->on('reply_msgs')->onDelete('cascade');
        //         $t->unsignedBigInteger('rule_id')->index()->foreign('rule_id')->references('id')->on('reply_msg_filters')->onDelete('cascade');
        //     });
        // }

        // // 自定义菜单
        // if (!Schema::hasTable('app_menu')) {
        //     Schema::create('app_menu', function (Blueprint $t) {
        //         $t->increments('id');
        //         $t->unsignedInteger('app_id')->foreign('app_id')->references('id')->on('apps')->onDelete('cascade');
        //         $t->unsignedInteger('parent_id')->comment('父类Id');
        //         $t->unsignedTinyInteger('weight')->comment('从低到高');
        //         $t->string('param', 256);
        //         $t->timestamps();
        //         $t->softDeletes();
        //     });
        // }
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('apps');
        // Schema::dropIfExists('app_domains');
        // Schema::dropIfExists('app_config');
        // Schema::dropIfExists('reply_words');
        // Schema::dropIfExists('reply_keyword');
        // Schema::dropIfExists('reply_qrcode_type');
        // Schema::dropIfExists('reply_qrcode_params');
        // Schema::dropIfExists('reply_msgs');
        // Schema::dropIfExists('reply_msg_filters');
        // Schema::dropIfExists('msgs_words');
        // Schema::dropIfExists('msgs_qrcodes');
        // Schema::dropIfExists('msgs_filters');
        // Schema::dropIfExists('app_menu');
    }
}
